package hnx.ca.main.processor;

import hnx.ca.entity.DecryptStringResult;
import hnx.ca.entity.EncryptStringResult;
import hnx.ca.entity.SignStringResult;
import hnx.ca.entity.SignXmlResult;
import hnx.ca.entity.VerifyStringResult;
import hnx.ca.entity.VerifyXmlResult;
import hnx.ca.security.StringVerifier;
import hnx.ca.security.XMLVerifier;
import hnx.ca.security.XmlSigner;
import hnx.ca.util.ErrorCodes;
import hnx.ca.util.ErrorMessages;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;

public class MainProcessor {

	public static VerifyXmlResult verifyXmlFile(String xmlUrl){
		return XMLVerifier.verifyXML(xmlUrl);
	}
	
	public static SignXmlResult signXmlFile(String inputUrl, String outputUrl){
		int errorCode = XmlSigner.signXmlFile(inputUrl, outputUrl);
		String errorMessage = "";
		if(errorCode == ErrorCodes.SIGN_XML.SUCCESS){
			errorMessage = ErrorMessages.SIGN_XML.SUCCESS;
		}
		if(errorCode == ErrorCodes.SIGN_XML.INVALID_INPUT_FILE_TYPE){
			errorMessage = ErrorMessages.SIGN_XML.INVALID_FILE_TYPE;
		}
		if(errorCode == ErrorCodes.SIGN_XML.IllegalAccessException){
			errorMessage = ErrorMessages.SIGN_XML.IllegalAccessException;
		}
		if(errorCode == ErrorCodes.SIGN_XML.IOException){
			errorMessage = ErrorMessages.SIGN_XML.IOException;
		}
		if(errorCode == ErrorCodes.SIGN_XML.GET_KEY_PAIR_EXCEPTION){
			errorMessage = ErrorMessages.SIGN_XML.KeyException;
		}
		if(errorCode == ErrorCodes.SIGN_XML.GET_KEY_STORE_EXCEPTION){
			errorMessage = ErrorMessages.SIGN_XML.OTHER_EXCEPTION;
		}
		if(errorCode == ErrorCodes.SIGN_XML.FAIL){
			errorMessage = ErrorMessages.SIGN_XML.FAIL;
		}
		
		return new SignXmlResult(errorCode, errorMessage, inputUrl, outputUrl);
	}
	
	public static EncryptStringResult encryptString(String encodedBase64String, String customerId){
		return StringVerifier.encryptString(encodedBase64String, customerId);
	}
	
	public static DecryptStringResult decryptString(String encodedBase64String){
		return StringVerifier.decryptString(encodedBase64String);
	}

	public static VerifyStringResult verifyString(String orignialText, String signature, String customerId) {
		return StringVerifier.verifyString(orignialText, signature, customerId);
	}
	
	
	public static void main(String args[]){
		String a = "30 81 89 02 81 81 00 93 00 33 85 6f c3 9d 6d d5 eb d5 38 29 ac 6f 55 89 5a ce b0 8b f0 f0 26 aa 0e aa 0f 87 07 82 04 e4 08 3a 97 a9 eb 8a 08 c7 2f 14 aa 8b 36 9e 27 9e 3e af 55 40 9c 7f 0d 05 cc e6 8a 17 0f 1b 57 46 ba 16 25 02 c3 27 40 67 62 da 51 f6 69 82 13 c3 c7 e8 82 94 ac 33 62 d7 4a 24 4d db 63 2c 48 35 c5 bc 75 44 99 24 fb 10 04 b1 e1 4f 8d dc 58 1c 97 71 8a 74 ca c6 c2 92 c8 60 79 c3 35 2f 0f 02 03 01 00 01";
		a = a.replaceAll(" ", "");
		System.out.println(a);
		String b = "30819f300d06092a864886f70d010101050003818d00";
		try {
			StringVerifier.getPublicKey(b+a);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

	public static SignStringResult signString(String base64String) {
		return StringVerifier.signString(base64String);
	}
}
